package com.uls.web.controller.czj;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.czj.domain.CzjFloor;
import com.uls.czj.domain.CzjRoom;
import com.uls.czj.service.ICzjFloorService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.czj.domain.CzjBuilding;
import com.uls.czj.service.ICzjBuildingService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 楼宇管理Controller
 * 
 * @author czj
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/czj/building")
public class CzjBuildingController extends BaseController
{
    @Autowired
    private ICzjBuildingService czjBuildingService;

    @Autowired
    private ICzjFloorService czjFloorService;

    /**
     * 查询楼宇管理列表
     */
    @PreAuthorize("@ss.hasPermi('czj:building:list')")
    @GetMapping("/list")
    public TableDataInfo list(CzjBuilding czjBuilding)
    {
        startPage();
        List<CzjBuilding> list = czjBuildingService.selectCzjBuildingList(czjBuilding);

        for (CzjBuilding czjBuilding1:list){
            CzjFloor czjFloor = new CzjFloor();
            czjFloor.setBuildingid(czjBuilding1.getBuildingid());
            List<CzjFloor> czjFloorList= czjFloorService.selectCzjFloorList(czjFloor);
            czjBuilding1.setHeight((long) czjFloorList.size());
            int a=0;
            int b=0;
            int c=0;
            czjBuilding1.setStatusUse((long) a);
            czjBuilding1.setStatusVacant((long) b);
            czjBuilding1.setStatusRepair((long) c);
            if (czjFloorList.size()!=0){
                for (CzjFloor czjFloor1:czjFloorList){
                    a+=czjFloor1.getStatusUse();
                    b+=czjFloor1.getStatusVacant();
                    c+=czjFloor1.getStatusRepair();
                }
                czjBuilding1.setStatusUse((long) a);
                czjBuilding1.setStatusVacant((long) b);
                czjBuilding1.setStatusRepair((long) c);
            }
            czjBuildingService.updateCzjBuilding(czjBuilding1);
        }

       /* CzjFloor czjFloor = new CzjFloor();
        List<CzjBuilding> list1 = new ArrayList<>();

        for (CzjBuilding czjBuilding1:list){
            czjFloor.setBuildingid(czjBuilding1.getBuildingid());

            list1.add(czjBuilding1);
        }*/
        return getDataTable(list);
    }

    /**
     * 导出楼宇管理列表
     */
    @PreAuthorize("@ss.hasPermi('czj:building:export')")
    @Log(title = "楼宇管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CzjBuilding czjBuilding)
    {
        List<CzjBuilding> list = czjBuildingService.selectCzjBuildingList(czjBuilding);
        ExcelUtil<CzjBuilding> util = new ExcelUtil<CzjBuilding>(CzjBuilding.class);
        util.exportExcel(response, list, "楼宇管理数据");
    }

    /**
     * 获取楼宇管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('czj:building:query')")
    @GetMapping(value = "/{buildingid}")
    public AjaxResult getInfo(@PathVariable("buildingid") Long buildingid)
    {
        return success(czjBuildingService.selectCzjBuildingByBuildingid(buildingid));
    }

    /**
     * 新增楼宇管理
     */
    @PreAuthorize("@ss.hasPermi('czj:building:add')")
    @Log(title = "楼宇管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CzjBuilding czjBuilding)
    {
        return toAjax(czjBuildingService.insertCzjBuilding(czjBuilding));
    }

    /**
     * 修改楼宇管理
     */
    @PreAuthorize("@ss.hasPermi('czj:building:edit')")
    @Log(title = "楼宇管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CzjBuilding czjBuilding)
    {
        return toAjax(czjBuildingService.updateCzjBuilding(czjBuilding));
    }

    /**
     * 删除楼宇管理
     */
    @PreAuthorize("@ss.hasPermi('czj:building:remove')")
    @Log(title = "楼宇管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{buildingids}")
    public AjaxResult remove(@PathVariable Long[] buildingids)
    {
        return toAjax(czjBuildingService.deleteCzjBuildingByBuildingids(buildingids));
    }
}
